<?php
/**
*@class The Class for Posts in a Forum of Wormpad.
*@description The Class for Posts in a Forum of Wormpad.This class contains data memebers and methods for operations on posts in a thread.
*@name Post
*@requires Requires connection.php for establishing connection to the database.
*/
class Post
{
    /**
    *@name postId_
    *@description Refers to the Id of the Post 
    *@type Integer
    *@memberOf Post
    *@private
    */
    private $postId_;   
    /**
    *@name threadId_
    *@description Refers to the Id of the Thread
    *@type Integer
    *@memberOf Post
    *@private
    */
    private $threadId_;
    /**
    *@name userId_
    *@description Refers to the Id of the User
    *@type : Integer
    *@memberOf Post
    *@private
    */
    private $userId_;
    /**
    *@name post_
    *@description Refers to the Post
    *@type : String
    *@memberOf Post
    *@private
    */
    private $post_;
    /**
    *@name isVisible_
    *@description Refers to the Visibility of the Post
    *@type : Boolean
    *@memberOf Post
    *@private
    */
    private $isVisible_;
    /**
    *@name timestamp_
    *@description Refers to the date of Post
    *@type : Timestamp
    *@memberOf Post
    *@private
    */
    private $timestamp_;
    
    /**
    *@description Function getPostId() is used to get Post Id of the Post inthe Thread
    *@return {Integer} The function returns Id of the Post
    *@memberOf Post
    */
    function getPostId()
    {
        return $this->postId_;  
    }
    /**
    *@description Function getThreadId() is used to get Thread Id of the Thread the post belongs to
    *@return {Integer} The function returns Id of the Thread
    *@memberOf Post
    */
    function getThreadId()
    {
        return $this->threadId_;    
    }
    /**
    *@description Function getUserId() is used to get User Id of the User who has posted the Post
    *@return {Integer} The function returns Id of the User
    *@memberOf Post
    */
    function getUserId()
    {
        return $this->userId_;  
    }
    /**
    *@description Function getPost() is used to get the Post
    *@return {String} The function returns the Post
    *@memberOf Post
    */
    function getPost()
    {
        return $this->post_;    
    }
    /**
    *@description Function getIsVisible() is used to get the Visibility Status of the Post
    *@return {Boolean} The function returns visibility of the Post
    *@memberOf Post
    */
    function getIsVisible()
    {
        return $this->isVisible_;   
    }
    /**
    *@description Function getTimestamp() is used to get Date of Creation of the Post
    *@return {Timestamp} The function returns Date of Creation of the Post
    *@memberOf Post
    */
    function getTimestamp()
    {
        return $this->timestamp_;   
    }
    
    
    /**
    *@description Function setPostId() is used to set Post Id of the Post
    *@param {Integer} postId The Function accepts postId, which is the Id of the Post
    *@memberOf Post
    */  
    function setPostId($postId)
    {
        $this->postId_ = $postId;;  
    }
    /**
    *@description Function setThreadId() is used to set Thread Id of the Thread
    *@param {Integer} threadId The Function accepts threadId, which is the Id of the Thread
    *@memberOf Post
    */
    function setThreadId($threadId)
    {
        $this->threadId_ = $threadId;   
    }
    /**
    *@description Function setUserId() is used to set User Id of the User
    *@param {Integer} userId The Function accepts userId, which is the Id of the User
    *@memberOf Post
    */
    function setUserId($userId)
    {
        $this->userId_ = $userId;   
    }
    /**
    *@description Function setPost() is used to set Post of a Thread
    *@param {String} post The Function accepts Post, which is the Post of the Thread
    *@memberOf Post
    */
    function setPost($post)
    {
        $this->post_ = $post;   
    }
    /**
    *@description Function setIsVisible() is used to set Visibilty of the Post
    *@param {Boolean} isVisible The Function accepts isVisible, which is the visibility of the Post
    *@memberOf Post
    */
    function setIsVisible($isVisible)
    {
        $this->isVisible_ = $isVisible; 
    }
    /**
    *@description Function setTimestamp() is used to set Date of Creation of the Post
    *@param {Timestamp} timestamp The Function accepts timestamp, which is the Date of Creation of the Post
    *@memberOf Post
    */
    function setTimestamp($timestamp)
    {
        $this->timestamp_ = $timestamp; 
    }   


    /**
    *@description Function load() is used to load Posts of a Thread
    *@param {Integer} threadId The Function accepts threadId, which is the Id of a Thread
    *@return {Post[]} The function returns Array of Objects of Posts
    *@memberOf Post
    */
    function load($threadId)
    {
        $posts=array();
        $query="SELECT * FROM post_details WHERE thread_id='".$threadId."'";
        $result=mysql_query($query);
        while($row=mysql_fetch_array($result,MYSQL_ASSOC))
        {
            $post=new Post();
            $post->postId_ = $row['post_id'];
            $post->threadId_ = $row['thread_id'];
            $post->userId_ = $row['user_id'];
            $post->post_ = $row['post'];
            $post->isVisible_ = $row['isVisible'];
            $post->timestamp_ = $row['timestamp'];
            $posts[] = $post;
        }
        return $posts;
    }
    
    /**
    *@description Function save() adds a new post to the Thread. The values are set using the set functions before calling hte save() function
    *@memberOf Post
    */
    function save()
    {
        $query="INSERT INTO post_details
        (
            thread_id,
            user_id,
            post,
            isVisible   
        )
        VALUES
        (
            '".$this->threadId_."',
            '".$this->userId_."',
            '".$this->post_."',
            true
        )"; 
        //print_r($query);
        mysql_query($query);
    }
    
    
/* 
 * ==============================================================================================================================
 * CHANGES/MODIFICATION/ADDTION BY ABHINEET STARTS  ////////////////////////////////////////////////////////////////////////////
 * ==============================================================================================================================
 */
    
    function getUsername($user_id)
    {
        $query="SELECT user_firstname FROM login_details WHERE user_id='".$user_id."'";
        $result=mysql_query($query);
        $row=mysql_fetch_array($result,MYSQL_ASSOC);
        $username=$row['user_firstname'];
        return $username;
    }
    function lastPostBy($thread_id)
    {
        $query="SELECT * FROM post_details WHERE thread_id='".$thread_id."';";
        $result=mysql_query($query);
        $count = array();
        while($row=mysql_fetch_array($result,MYSQL_ASSOC))
        {
            $count[]=$row['user_id'];
        }
        return $count;
    }
	function deletePost($PostId)
	{
		$query="UPDATE post_details SET isVisible=0 WHERE post_id='".$PostId."'";
		mysql_query($query);
		return mysql_insert_id();
	}    

}

?>